﻿using System;
using System.Collections.Generic;
using System.Text;
using WeiboFilter.Models;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace WeiboFilter.DAL
{
    public class StatusDAL
    {
        private string sp_insertWeiBoStatus = "dbo.insertWeiBoStatus";
        private string sp_getTopWeiBoStatuses = "dbo.getWeiBoStatuses";
        private string sp_updateWeiBoStatusState = "dbo.updateWeiBoStatusState";
        private string sp_getWeiBoStatusById = "dbo.getWeiBoStatusById";



        public Status GetStatusById(string id)
        {
            Status s = new Status();
            SqlParameter[] Params = new SqlParameter[]{
                    new SqlParameter("@id",id)
            };
            using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlCommon.connString, CommandType.StoredProcedure, sp_getWeiBoStatusById, Params))
            {
                sdr.Read();
                s.id = sdr.GetString(0);
                s.text = sdr.GetString(1);
                s.created_at = sdr.GetString(2);
                string author_id = sdr.GetString(3);
                UserDAL uD = new UserDAL();
                s.author = uD.GetWeiBoUserById(author_id);
                s.location = string.Format("http://api.t.sina.com.cn/{0}/statuses/{1}", s.author.id, s.id);
            }

            return s;
        }

        public int InsertWeiBoStatus(Status s, string customerId)
        {
            SqlParameter[] Params = new SqlParameter[]{
                    new SqlParameter("@id",s.id),
                new SqlParameter("@txt",s.text),
                new SqlParameter("@created_at",s.created_at),
                new SqlParameter("@author_id",s.author.id),
                new SqlParameter("@customerId",customerId)
                };
            return SqlHelper.ExecuteNonQuery(SqlCommon.connString, CommandType.StoredProcedure, sp_insertWeiBoStatus, Params);
        }

        public List<Status> GetTopWeiBoStatuses(int n, string customerId)
        {
            List<Status> sL = new List<Status>();
            SqlParameter[] Params = new SqlParameter[]{
                    new SqlParameter("@count",n),
                    new SqlParameter("@customerId",customerId)
            };
            using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlCommon.connString, CommandType.StoredProcedure, sp_getTopWeiBoStatuses, Params))
            {
                while (sdr.Read())
                {
                    Status s = new Status();
                    s.id = sdr.GetString(0);
                    s.text = sdr.GetString(1);
                    s.created_at = sdr.GetString(2);
                    string author_id = sdr.GetString(3);
                    UserDAL uD = new UserDAL();
                    s.author = uD.GetWeiBoUserById(author_id);
                    s.location = string.Format("http://api.t.sina.com.cn/{0}/statuses/{1}", s.author.id, s.id);
                    sL.Add(s);
                }
            }
            return sL;
        }

        //改变微博状态，在当前用户下
        public int UpdateWeiBoStatusState(string statusId, int state, string customerId, double point)
        {
            SqlParameter[] Params = new SqlParameter[]{
                new SqlParameter("@statusId",statusId),
                new SqlParameter("@type",state),
                new SqlParameter("@c_id",customerId),
                new SqlParameter("@point",point)
                };
            return SqlHelper.ExecuteNonQuery(SqlCommon.connString, CommandType.StoredProcedure, sp_updateWeiBoStatusState, Params);

        }

    }
}
